package com.anbinh.doctor.fragment.account;

import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.anbinh.doctor.R;
import com.anbinh.doctor.adapter.ListCommentAdapter;
import com.anbinh.doctor.model.ItemComment;
import com.anbinh.doctor.model.ResponseComment;
import com.anbinh.doctor.model.UserLogin;
import com.anbinh.doctor.request.controller.ServerController;
import com.anbinh.doctor.service.request.OnRequest;
import com.anbinh.doctor.utils.IRcallBak;
import com.anbinh.doctor.utils.Utils;
import com.lsjwzh.widget.materialloadingprogressbar.CircleProgressBar;

public class FragmentListComment extends Fragment implements IRcallBak {
	private static FragmentListComment fragment = null;
	private View mView;
	private CircleProgressBar loadding;
	private ListView mListView;
	private ListCommentAdapter adapter;
	private List<ItemComment> mList;
	private ItemComment mCurrentComment;  
	
	
	private View fotter;
	private int currentPage = 0;

	public static FragmentListComment newInstance() {
		if (fragment == null) {
			fragment = new FragmentListComment();
		}
		return fragment;
	}

	@Override
	public void onAttach(Activity activity) {
		// TODO Auto-generated method stub
		super.onAttach(activity);
	}

	@Override
	public void onResume() {
		super.onResume();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		currentPage = 0;
		adapter = null;
		mView = inflater.inflate(R.layout.fragment_list_comment, null);
		return mView;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		mListView = (ListView) mView.findViewById(R.id.listDrag);
		fotter = getActivity().getLayoutInflater().inflate(
				R.layout.footer_loadmore, null);
		fotter.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				onLoadListComment();
			}
		});

		loadding = (CircleProgressBar) mView.findViewById(R.id.loadding);
		onLoadListComment();
		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				mCurrentComment = mList.get(position);
				Bundle bundle = new Bundle();
				bundle.putInt(Utils.KEY, Utils.COMMENT_DETAILT);
				bundle.putBoolean(Utils.CUSTOMBAR, false);
				bundle.putString(Utils.TITLE,
						getResources().getString(R.string.chi_tiet_binh_luan));
				Utils.getNewIntent(getActivity(), bundle);
			}

		});

	}

	public ItemComment getCurrentComment() {
		return mCurrentComment;
	}

	public void onLoadListComment() {
		loadding.setVisibility(View.VISIBLE);
		UserLogin userLogin = Utils.getAccountModel(getActivity(),
				Utils.KEY_USER);
		ServerController.getInstance(getActivity()).requestGetListComment(
				userLogin.getData().getToken(), currentPage,
				new OnRequest<ResponseComment>() {

					@Override
					public void onSuccess(ResponseComment data) {
						if (getActivity() == null)
							return;

						loadding.setVisibility(View.INVISIBLE);
						if (data.getResponsecode() >= 0) {
							currentPage = data.getData().getPaging()
									.getCurrentPage();
							int lastPage = data.getData().getPaging()
									.getLastPage();

							if (currentPage < lastPage) {
								mListView.addFooterView(fotter);
							} else if (mListView.getFooterViewsCount() > 0) {
								mListView.removeFooterView(fotter);
							}

							if (adapter == null) {
								mList = data.getData().getComments();
								adapter = new ListCommentAdapter(getActivity(),
										R.layout.item_comment, mList);
								mListView.setAdapter(adapter);
							} else {
								mList.addAll(data.getData().getComments());
								adapter.notifyDataSetChanged();
							}
						}

					}

					@Override
					public void onError(int errorCode, String message) {
						// TODO Auto-generated method stub

					}
				});

	}

	@Override
	public void onDisplay(int type, Bundle data) {

	}

}
